package com.wh.wisdomsite.xbox.core.event;

import org.springframework.aop.support.AopUtils;
import org.springframework.core.GenericTypeResolver;

/**
 * Created with IntelliJ IDEA.
 * User: yl
 * Date: 13-2-22
 * Time: 上午11:49
 * To change this template use File | Settings | File Templates.
 */
public class GenericXboxEventAdapter implements SmartXboxEventListener {

    private final XboxEventListener delegate;

    public GenericXboxEventAdapter(XboxEventListener delegate) {
        this.delegate = delegate;
    }

    /**
     * 判断是否支持某事件
     * @param eventType
     * @return
     */
    @Override
    public boolean supportsEventType(Class<? extends XboxEvent> eventType) {
        Class typeArg = GenericTypeResolver.resolveTypeArgument(this.delegate.getClass(), XboxEventListener.class);
        if (typeArg == null || typeArg.equals(XboxEvent.class)) {
            Class targetClass = AopUtils.getTargetClass(this.delegate);
            if (targetClass != this.delegate.getClass()) {
                typeArg = GenericTypeResolver.resolveTypeArgument(targetClass, XboxEventListener.class);
            }
        }
        return (typeArg == null || typeArg.isAssignableFrom(eventType));
    }

    /**
     * 处理事件
     * @param event
     */
    @Override
    public void handleEvent(XboxEvent event) {
        this.delegate.handleEvent(event);
    }

    /**
     * 获取顺序
     * @return
     */
    @Override
    public int getOrder() {
        return Integer.MAX_VALUE;
    }
}
